/**
 *查询日报表与搜索框的模糊查询
 */
var deviceId = getUrlParam('deviceId'),
    createTime=getUrlParam('createTime'),
    deviceName=getUrlParam('deviceName'),
    reportsType=getUrlParam('reportsType'),
    createTimeFormat=reportsType=='1'?new Date(parseInt(createTime)).Format("yyyy-MM-dd"):createTime;
    reportsType=='1'?$(".reportsType").text("日报表"):$(".reportsType").text("月报表");
    $(".reportsh4").html(deviceName);//获取该报表的名称
var Grid = BUI.Grid,
    Store = BUI.Data.Store,
    columns = [
        {
            title: '序号', dataIndex: 'flowDailyReportId',width:'4%', renderer: function (value,obj,index) {
                return index+1;
            }
        },
        {title: '上日读数', dataIndex: 'yesterdayReadings',width:'12%', renderer: function (value) {
                return (value+ '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');

            }},
        {title: '今日读数', dataIndex: 'todayReadings',width:'12%', renderer: function (value) {
                return (value+ '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');

            }},
        {title: '水量', dataIndex: 'waterAmount',width:'12%', renderer: function (value) {
                return (value+ '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');

            }},
        {title: '系数', dataIndex: 'factor',width:'12%'},
        {title: '价格', dataIndex: 'price',width:'12%'},
        {title: '时间', dataIndex: 'createTime',width:'12%', renderer: function (value) {
                return new Date(value).Format("yyyy-MM-dd hh:mm:ss");
            }
        },
        {title: '备注', dataIndex: 'remark',width:'12%', renderer: function (value) {
                return value==""||value==null?"____":value;
            }
        },
        {
            title: '操作', dataIndex: 'flowDailyReportId',width:'12%', renderer: function (value) {
                var option1 ='<a href="javascript: editBtn(' + value + ');">编辑</a>';
                var option2= '<a href="javascript: delOneBtn(' + value + ');">删除</a>';
                return option1 + "&nbsp;&nbsp;&nbsp;&nbsp;" + option2;
            }
        }
    ];

//分页查询
var store = new Store({
        url: '/rest/web/flowDailyReport/selectDeviceDailyReportPage',
        autoLoad: true, //自动加载数据
        params: { //配置初始请求的参数
            length: '15',
            start: '0',
            deviceId:deviceId,
            createTime:createTimeFormat
        },
        pageSize: 15,	// 配置分页数目
        root: 'data.list',
        totalProperty: 'data.totalCount'
    }),
    grid = new Grid.Grid({
        render: '#grid',
        columns: columns,
        loadMask: true, //加载数据时显示屏蔽层
        store: store,
        plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
        // 底部工具栏
        bbar: {
            pagingBar: true
        }
    });

grid.render();
//查询总水量的数值
(function(){
    $.ajax({
        type:'GET',
        dataType:'json',
        url:'/rest/web/flowDailyReport/queryDeviceFlowTotal',
        data:{
            deviceId:deviceId,
            dayTime:createTimeFormat
        },
        success:function(data){
            $(".totalWater-title").text((data.data+ '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,'));
        }
    })
})();
/**删除单个**/
function delOneBtn(value) {
    BUI.Message.Confirm('<h4 class="title-p">提示信息</h4><p>您确认要删除吗？</p>', function () {
        $.ajax({
            type: "POST",
            url: "/rest/web/flowDailyReport/deleteFlowDailyReport",
            dataType: "json",
            data: {
                flowDailyReportId: value
            },
            success: function (data) {
                var obj = new Object();
                obj.start = 0; //返回第一页
                store.load(obj);
            }
        });
    }, 'question');
}

/**删除多个**/
function delBtn() {
    var selections = grid.getSelection();
    var ids = new Array();
    for (var i = 0; i < selections.length; i++) {
        ids[i] = selections[i].flowDailyReportId.toString()
    }
    BUI.Message.Confirm('<h4 class="title-p">提示信息</h4><p>您确认要删除吗？</p>', function () {
        $.ajax({
            type: "POST",
            url: "/rest/web/flowDailyReport/deleteFlowDailyReports",
            dataType: "json",
            data: {
                ids: ids
            },
            success: function (data) {
                var obj = new Object();
                obj.start = 0; //返回第一页
                store.load(obj);
            }
        });
    }, 'question');
}
/*------------------------------------------------新增表单处理start------------------------------------------------*/
var addForm = new BUI.Form.Form({
    srcNode: '#addForm',
    submitType: 'ajax',
    callback: function (data) {
        var obj = new Object();
        obj.start = 0; //返回第一页
        store.load(obj);
        addDialog.close();
    }
}).render();

var addDialog = new BUI.Overlay.Dialog({
    title: '添加',
    width: 450,
    height: 450,
    contentId: 'addContent',
    success: function () {
        addAjaxBtn();
    }
});
function  addAjaxBtn() {
    var yesterdayReadings= $('#addForm input[name="yesterdayReadings"]').val(),
        todayReadings=$('#addForm input[name="todayReadings"]').val(),
        factor= $('#addForm input[name="factor"]').val(),
        price = $('#addForm input[name="price"]').val(),
        waterAmount= $('#addForm input[name="waterAmount"]').val(),
        createTime1=$('#addForm input[name="createTime"]').val(),
        remark=$('#addForm textarea[name="remark"]').val(),
        timestamp = (new Date(createTime1));
    var params = {deviceId:deviceId,yesterdayReadings: yesterdayReadings, todayReadings: todayReadings,price: price,factor:factor,remark:remark,createTime:timestamp,waterAmount:waterAmount};
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/rest/web/flowDailyReport/insertFlowDailyReport",
        data:params,
        success: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            addDialog.close();
        }
    })
}
function addBtn(){
    addForm.clearFields();
    addDialog.show();
}
/**
 *编辑设置
 */
function editBtn(id){
    $.ajax({
        type: "GET",
        dataType: "json",
        url: "/rest/web/flowDailyReport/selectFlowDailyReportDetail",
        data: {flowDailyReportId: id},
        success: function (data) {
            var form = $("#editForm")[0];
            BUI.FormHelper.setFields(form, data.data);
            $("input[name='createTime']").val(new Date(data.data.createTime).Format("yyyy-MM-dd hh:mm:ss"));
            editDialog.show();
        }
    });
}
var editForm = new BUI.Form.Form({
    srcNode: '#editForm',
    submitType: 'ajax',
    callback: function (data) {

    }
});
editForm.render();
var editDialog = new BUI.Overlay.Dialog({
    title: '编辑',
    width: 450,
    height: 450,
    contentId: 'editContent',
    success: function () {
        if (editForm.isValid()) {
            editAjaxBtn();
        } else {
            editForm.valid();
        }
    }
});
function editAjaxBtn() {
    var yesterdayReadings= $('#editForm input[name="yesterdayReadings"]').val(),
        todayReadings=$('#editForm input[name="todayReadings"]').val(),
        factor= $('#editForm input[name="factor"]').val(),
        price = $('#editForm input[name="price"]').val(),
        createTime1=$('#editForm input[name="createTime"]').val(),
        remark=$('#editForm textarea[name="remark"]').val(),
        waterAmount= $('#editForm input[name="waterAmount"]').val(),
        flowDailyReportId=$('#editForm input[name="flowDailyReportId"]').val(),
        timestamp = (new Date(createTime1));
    var params = {deviceId:deviceId,yesterdayReadings: yesterdayReadings, todayReadings: todayReadings,price: price,factor:factor,remark:remark,createTime:timestamp,flowDailyReportId:flowDailyReportId,waterAmount:waterAmount};
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/rest/web/flowDailyReport/updateFlowDailyReport",
        data:params,
        success: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            editDialog.close();
        }
    })
}
//点击导出按钮，导出数据
function exportData(){
    window.location.href = "/rest/web/flowDailyReport/excelDeviceDailyDownload?deviceId=" + deviceId+"&createTime="+createTimeFormat;
}
//点击后退一页
function backReports(){
    reportsType=='1'?window.location.href = "/rest/web/flowDailyReport/toDayReports":window.location.href = "/rest/web/flowDailyReport/toMonthReports"
}